package com.jzg.jcptadmin.presenter;

import com.blankj.utilcode.utils.StringUtils;
import com.jzg.jcptadmin.Utils.MD5Utils;
import com.jzg.jcptadmin.app.AppContext;
import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.CarTypeSelectModel;
import com.jzg.jcptadmin.viewinterface.ICarDiffInterface;

import java.util.HashMap;
import java.util.Map;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static com.jzg.jcptadmin.app.AppContext.isNetWork;

/**
 * 车型选择
 * Created by zealjiang on 2016/11/4 21:05.
 * Email: zealjiang@126.com
 */

public class CarConfigParaSelectPresenter extends BasePresenter<ICarDiffInterface> {

    private DataManager dataManager;
    private ICarDiffInterface mView;
    private Subscription mSubscription;

    public CarConfigParaSelectPresenter(DataManager dataManager) {
        super();
        this.dataManager = dataManager;
    }


    @Override
    public void attachView(ICarDiffInterface mvpView) {
        super.attachView(mvpView);
        this.mView = mvpView;

    }
    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null) mSubscription.unsubscribe();
    }

    /**
     * 请求差异项配置
     * 车系Id、车架号、排量、变速器（1自动，2手动，3电动）、驱动方式（3四驱，4两驱）、出厂日期、车辆铭牌
     * @author libo
     * @time 2017/3/20
     */
    public void requestCarDiff(String modelid,String vin,String pl,String bsx,String qdfs,String productYear,String nameplate){
        //处理Null
        modelid = StringUtils.null2Length0(modelid);
        vin = StringUtils.null2Length0(vin);
        pl = StringUtils.null2Length0(pl);
        bsx = StringUtils.null2Length0(bsx);
        qdfs = StringUtils.null2Length0(qdfs);
        productYear = StringUtils.null2Length0(productYear);
        nameplate = StringUtils.null2Length0(nameplate);

        checkViewAttached();
        if(!isNetWork){
            mView.showError("对不起，您的网络状况不佳，请确保已经连接网络！");
            mView.fail("");
            return;
        }

        Map<String, String> params = new HashMap<>();
        params.put("modelid", modelid);
        params.put("vin", vin);
        params.put("pl", pl);
        params.put("bsx", bsx);
        params.put("qdfs", qdfs);
        params.put("productYear", productYear);
        params.put("noticeNo", nameplate);
        params.put("UserId",AppContext.getContext().getUser().getUserId()+"");
        params.put("tokenID", "6");
        //加sign
        params = MD5Utils.encryptParams(params);
        mView.showDialog();

        mSubscription = dataManager.getCarDiff(params)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io()).subscribe(new Observer<CarTypeSelectModel>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissDialog();
                        if(e!=null&& mView!=null){
                            mView.showError(e.toString());
                            mView.fail("");
                        }
                    }

                    @Override
                    public void onNext(CarTypeSelectModel responseJson) {
                        int status = responseJson.getStatus();
                        if (status == 100) {
                            mView.succeed(responseJson);
                        }else {
                            mView.showError(responseJson.getMsg());
                            mView.fail(responseJson.getMsg());
                        }
                    }
                });
    }


}
